package com.hancheng.commoncloud.framework.result;

import com.hancheng.commoncloud.framework.result.exception.ErrorCode;
import com.hancheng.commoncloud.framework.result.exception.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author 金笛
 * @date 2022/1/23 16:05
 */
@Data
@ApiModel(description = "配合前端统一返回结果的格式工具类")
public class Response<T> {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码  ${@link com.hancheng.commoncloud.framework.result.exception.ResultCode}
     */
    @ApiModelProperty(notes = "状态码")
    private Integer resCode;

    /**
     * 提示信息
     */
    @ApiModelProperty(notes = "提示信息")
    private String resDesc;

    /**
     * 返回包装类型
     */
    @ApiModelProperty(notes = "数据集合")
    private T data;

    private Response() {
    }

    private Response(Integer resCode, String resDesc) {
        this.resCode = resCode;
        this.resDesc = resDesc;
    }

    private Response(ResultCode resultCode) {
        this.resCode = resultCode.getCode();
        this.resDesc = resultCode.getMessage();
    }

    public static <T> Response<T> ok() {
        return new Response<>(ResultCode.SUCCESS);
    }

    public static <T> Response<T> ok(T data) {
        Response<T> response = new Response<>(ResultCode.SUCCESS);
        response.data = data;
        return response;
    }

    public static <T> Response<T> error() {
        return error(ResultCode.FAILED);
    }

    /**
     * 支持接收自定义返回结果枚举
     */
    public static <T> Response<T> error(ErrorCode resultCode) {
        return error(resultCode.getCode(), resultCode.getMessage());
    }

    public static <T> Response<T> error(String message) {
        return error(ResultCode.FAILED.getCode(), message);
    }

    public static <T> Response<T> error(Integer code, String message) {
        return new Response<>(code, message);
    }

    public Response msg(String message) {
        this.resDesc = message;
        return this;
    }
}
